package com.kongwg.java;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author kongwg
 * @create 2020-12-19 15:39
 */
public class RandomAccessFileTest {

    @Test
    public void RandemAccessFileCopyTest() {
        RandomAccessFile raf = null;
        RandomAccessFile raf1 = null;
        try {
            raf = new RandomAccessFile(new File("爱情与友情.jpg"), "r");
            raf1 = new RandomAccessFile(new File("爱情与友情1.jpg"), "rw");

            byte[] buffer = new byte[1024];
            int len;
            while ((len = raf.read(buffer)) != -1) {
                raf1.write(buffer, 0, len);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (raf1 != null) {

                try {
                    raf1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }


}
